/**
 * Copyright (c) 2024 Huawei Technologies Co., Ltd.
 * This file is a part of the CANN Open Software.
 * Licensed under CANN Open Software License Agreement Version 1.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

/*!
 * \file copy_cube_in_params.h
 * \brief copy cube in variable manager module
 */

#ifndef IMPL_MATMUL_MODULES_STAGE_COPY_CUBE_IN_PARAMS_MANAGER_H
#define IMPL_MATMUL_MODULES_STAGE_COPY_CUBE_IN_PARAMS_MANAGER_H

#include "../../param/matmul_var.h"

namespace matmul {
template <typename IMPL, const auto& MM_CFG, class INPUT_TYPE, typename = void>
class CopyCubeInParams {
    MATMUL_USE_MODULE_ON(MatmulVar, INPUT_TYPE::TAG);
    MATMUL_USE_MODULE_ON(MatmulShapeInfo, INPUT_TYPE::TAG);
    using TransT = typename INPUT_TYPE::TRANS_T;
    using SrcT = typename INPUT_TYPE::T;
public:
    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetStepCol() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetStepKa();
        } else {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetStepM();
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetStepRow() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetStepM();
        } else {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetStepKa();
        }
    }

    __aicore__ inline int32_t GetBufferPos() const
    {
        return MATMUL_CONST_PARAM_VAR.isA1KFullLoad_ ?
            MATMUL_CONST_PARAM_VAR.stepMIdx_: MATMUL_CONST_PARAM_VAR.stepKaIdx_;
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetSingleHeightAlign() const
    {
        if constexpr (IS_TRANS) {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetSingleHeight(), c0Size_);
        } else {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetSingleHeight(), BLOCK_CUBE);
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetSingleWidthAlign() const
    {
        if (!IS_TRANS || IsSameTypeV<TransT, int8_t>) {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetSingleWidth(), c0Size_);
        } else {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetSingleWidth(), BLOCK_CUBE);
        }
    }

    __aicore__ inline int32_t GetSingleSize() const
    {
        return MATMUL_MODULE(MatmulShapeInfo)->GetSingleWidth() * MATMUL_MODULE(MatmulShapeInfo)->GetSingleHeight();
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetSingleSizeAlign() const
    {
        return GetSingleWidthAlign<IS_TRANS>() * GetSingleHeightAlign<IS_TRANS>();
    }

    __aicore__ inline int32_t GetBufferSize()
    {
#if __CCE_AICORE__ == 310
        if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
            return GetOrgSize();
        } else {
            return GetBaseSize();
        }
#else
        return GetBaseSize();
#endif
    }

private:
    constexpr static int32_t c0Size_ = AuxGetC0Size<TransT>();

    __aicore__ inline int32_t GetBaseHeightAlign() const
    {
        if constexpr (IsSameTypeV<SrcT, float>) {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetBaseHeight(), BLOCK_CUBE);
        } else if constexpr (IsTypeOneOfV<SrcT, int8_t, int4b_t> && INPUT_TYPE::isTrans) {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetBaseHeight(), c0Size_);
        } else {
            return MATMUL_MODULE(MatmulShapeInfo)->GetBaseHeight();
        }
    }

    __aicore__ inline int32_t GetBaseWidthAlign() const
    {
        if constexpr (INPUT_TYPE::isTrans && IsSameTypeV<SrcT, float>) {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetBaseWidth(), BLOCK_CUBE);
        } else if constexpr (IsTypeOneOfV<SrcT, float, int8_t, int4b_t>) {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetBaseWidth(), c0Size_);
        } else {
            return MATMUL_MODULE(MatmulShapeInfo)->GetBaseWidth();
        }
    }

    __aicore__ inline int32_t GetOrgHeightAlign()
    {
        return Align<int32_t>(MATMUL_MODULE(MatmulShapeInfo)->GetOrgHeight(), BLOCK_CUBE);
    }

    __aicore__ inline int32_t GetOrgWidthAlign()
    {
        return Align<int32_t>(MATMUL_MODULE(MatmulShapeInfo)->GetOrgWidth(), c0Size_);
    }

    __aicore__ inline int32_t GetBaseSize()
    {
        if constexpr (INPUT_TYPE::format == CubeFormat::VECTOR && !IsTypeOneOfV<SrcT, int8_t, int4b_t, float>) {
            return MATMUL_MODULE(MatmulShapeInfo)->GetBaseWidth();
        } else {
            return GetBaseHeightAlign() * GetBaseWidthAlign();
        }
    }

    __aicore__ inline int32_t GetOrgSize()
    {
        return GetOrgHeightAlign() * GetOrgWidthAlign();
    }
};

template <typename IMPL, const auto& MM_CFG, class INPUT_TYPE>
class CopyCubeInParams<IMPL, MM_CFG, INPUT_TYPE, enable_if_t<INPUT_TYPE::TAG == InputTypeTag::B>> {
    MATMUL_USE_MODULE_ON(MatmulVar, InputTypeTag::B);
    MATMUL_USE_MODULE_ON(MatmulShapeInfo, INPUT_TYPE::TAG);
    using TransT = typename INPUT_TYPE::TRANS_T;
    using SrcT = typename INPUT_TYPE::T;
public:
    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetStepCol() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetStepKb();
        } else {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetStepN();
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetStepRow() const
    {
        if constexpr (IS_TRANS) {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetStepN();
        } else {
            return MATMUL_CONST_PARAM_VAR.tiling_.GetStepKb();
        }
    }

    __aicore__ inline int32_t GetBufferPos() const
    {
        return MATMUL_CONST_PARAM_VAR.isB1KFullLoad_ ? MATMUL_CONST_PARAM_VAR.stepNIdx_:
            MATMUL_CONST_PARAM_VAR.stepKbIdx_;
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetSingleHeightAlign() const
    {
        if constexpr (IS_TRANS || IsSameTypeV<TransT, int8_t>) {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetSingleHeight(), c0Size_);
        } else {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetSingleHeight(), BLOCK_CUBE);
        }
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetSingleWidthAlign() const
    {
        if constexpr (IS_TRANS) {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetSingleWidth(), BLOCK_CUBE);
        } else {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetSingleWidth(), c0Size_);
        }
    }

    __aicore__ inline int32_t GetSingleSize() const
    {
        return MATMUL_MODULE(MatmulShapeInfo)->GetSingleWidth() * MATMUL_MODULE(MatmulShapeInfo)->GetSingleHeight();
    }

    template <bool IS_TRANS = false>
    __aicore__ inline int32_t GetSingleSizeAlign() const
    {
        return GetSingleWidthAlign<IS_TRANS>() * GetSingleHeightAlign<IS_TRANS>();
    }

    __aicore__ inline int32_t GetBufferSize()
    {
#if __CCE_AICORE__ == 310
        if constexpr (PhyPosIsUB(INPUT_TYPE::pos)) {
            return GetOrgSize();
        } else {
            return GetBaseSize();
        }
#else
        return GetBaseSize();
#endif
    }

private:
    constexpr static int32_t c0Size_ = AuxGetC0Size<TransT>();

    __aicore__ inline int32_t GetBaseHeightAlign() const
    {
        if constexpr (IsSameTypeV<SrcT, float>) {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetBaseHeight(), BLOCK_CUBE);
        } else if constexpr (IsTypeOneOfV<SrcT, int8_t, int4b_t>) {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetBaseHeight(), c0Size_);
        } else {
            return MATMUL_MODULE(MatmulShapeInfo)->GetBaseHeight();
        }
    }

    __aicore__ inline int32_t GetBaseWidthAlign() const
    {
        if constexpr (IsSameTypeV<SrcT, float> || (IsTypeOneOfV<SrcT, int8_t, int4b_t> && !INPUT_TYPE::isTrans)) {
            return Align(MATMUL_MODULE(MatmulShapeInfo)->GetBaseWidth(), c0Size_);
        } else {
            return MATMUL_MODULE(MatmulShapeInfo)->GetBaseWidth();
        }
    }

    __aicore__ inline int32_t GetOrgHeightAlign()
    {
        return Align<int32_t>(MATMUL_MODULE(MatmulShapeInfo)->GetOrgHeight(), BLOCK_CUBE);
    }

    __aicore__ inline int32_t GetOrgWidthAlign()
    {
        return Align<int32_t>(MATMUL_MODULE(MatmulShapeInfo)->GetOrgWidth(), c0Size_);
    }

    __aicore__ inline int32_t GetBaseSize()
    {
        if constexpr (INPUT_TYPE::format == CubeFormat::VECTOR && !IsTypeOneOfV<SrcT, int8_t, int4b_t, float>) {
            return MATMUL_MODULE(MatmulShapeInfo)->GetBaseWidth();
        } else {
            return GetBaseHeightAlign() * GetBaseWidthAlign();
        }
    }

    __aicore__ inline int32_t GetOrgSize()
    {
        return GetOrgHeightAlign() * GetOrgWidthAlign();
    }
};
}      // namespace matmul
#endif // IMPL_MATMUL_MODULES_STAGE_COPY_CUBE_IN_PARAMS_MANAGER_H
